#!/usr/bin/env bash
# Determine the home directory of PLEXIL by the location of this file.

# Copyright (c) 2006-2016, Universities Space Research Association (USRA).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Universities Space Research Association nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY USRA ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL USRA BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Strip './' and '/.' out of the input string, echo result to stdout.
# $1 is the input string.
handle_dot()
{
    # echo 'handle_dot_slash' "${1}" 1>&2 # debug

    local thestring="${1}"

    # strip intermediate 
    local newstring="${thestring/\/.\///}"
    while [ -n "${thestring}" ] && [ "${newstring}" != "${thestring}" ]
    do
	thestring="${newstring}"
	newstring="${thestring/\/.\///}"
    done

    # strip leading
    # should be at most one
    if [ './' == "${thestring:0:2}" ]
    then
	thestring="${thestring:2}"
    fi

    # strip trailing
    # should be at most one
    if [ '/.' = "${thestring:(-2)}" ]
    then
	thestring="${thestring:0:${#thestring}-2}"
    fi

    echo "${thestring}"
    return
}

# Strip '/..' out of the input string, echo result to stdout.
# $1 is the input string.
handle_dot_dot()
{
    # handle intervening
    local val="${1}"
    local newval=`echo "${val}" | sed -e 's|/[^\./]*/\.\./|/|'`
    while [ -n "${val}" ] && [ "${val}" != "${newval}" ]
    do
	val="${newval}"
	newval=`echo "${val}" | sed -e 's|/[^\./]*/\.\./|/|'`
    done

    # handle trailing
    val=`echo "${val}" | sed -e 's|/[^\./]*/\.\.$||'`

    echo "${val}"
    return
}

# Resolve a relative pathname to an absolute one, and echo it to stdout.
# $1 is the relative name.
# $2, if supplied, is the directory it's relative to (default $PWD).
to_absolute()
{
    # echo 'to_absolute' "${1}" 1>&2 # debug

    # strip trailing slash
    local thepath="${1%\/}"

    # strip '.'
    thepath=`handle_dot "${thepath}"`

    # strip '..'
    thepath=`handle_dot_dot "${thepath}"`

    if [ '/' == "${thepath:0:1}" ]
    then
	# already absolute
	echo "${thepath}"
	return
    fi

    # Below this line is a relative pathname 
    local base
    if [ -n "${2}" ]
    then
	base=`to_absolute "${2}"`
    fi

    if [ -z "${base}" ]
    then
	base="${PWD}"
    fi

    if [ -z "${thepath}" ] || [ '.' == "${thepath}" ]
    then
	echo "${base}"
    else 
	# Simply append to base and handle leading .. on thepath
	handle_dot_dot "${base}/${thepath}"
    fi
    return
}

# If the path is the name of a soft link, echo the link's (ultimate) target to stdout,
# else echo the argument.
# $1 is the path to chase. 
follow_soft_links()
{
    local path=`to_absolute "${1}"`
    while [ -L "${path}" ]
    do
	local dir=`dirname "${path}"`
	local target=`readlink "${path}"`
	path=`to_absolute "${target}" "${dir}"`
    done
    echo "${path}"
    return
}

mypath=`follow_soft_links "${BASH_SOURCE[${#BASH_SOURCE[@]}-1]}"`

echo "${mypath%\/*\/*}"
exit 0
